import * as Cesium from 'cesium/Cesium';
import { createDataSource } from '../tools/DataSourceUtil'
import * as Plots from '../plot/index'
import PlotUtils from '../utils/PlotUtils'

class PlotDraw {

    constructor(viewer, params) {
        if (viewer && viewer instanceof Cesium.Viewer) {
            this.viewer = viewer;
        } else {
            throw new Error("传入的不是地图对象！");
        }
        this.options = params || {};
        /**
         * 交互点
         * @type {null}
         */
        this.points = null;
        /**
         * 当前标绘工具
         * @type {null}
         */
        this.plot = null;
        /**
         * 当前要素
         * @type {null}
         */
        this.feature = null;
        /**
         * 标绘类型
         * @type {null}
         */
        this.plotType = null;
        /**
         * 当前标绘参数
         * @type {null}
         */
        this.plotParams = null;
        /**
         * 当前地图视图
         * @type {Element}
         */
        this.handler = null;
        /**
         * 地图双击交互
         * @type {null}
         */
        this.dblClickZoomInteraction = null;

        /**
         * 第一次交互标记
         * @type {boolean}
         */
        this.first = true;

        /**
         * draw交互工具
         * @type {null}
         * @private
         */
        this.drawInteraction_ = null;
        /**
     * 创建图层名称
     * @type {string}
     */
        this.layerName =
            this.options && this.options["layerName"]
                ? this.options["layerName"]
                : "POLT_DEFAULT_LAYERNAME";

        /**
         * plot数据源
         * @type {null}
         * @private
         */
        this.plotDataSource = createDataSource(this.viewer, this.layerName, {
            create: true,
        })

    }



    /**
     * 创建Plot
     * @param type
     * @param points
     * @param _params
     * @returns {*}
     */
    createPlot(type, points, _params) {
        let params = _params || {};
        switch (type) {
            case "ATTACK_ARROW":
                return new Plots.AttackArrow(points, params);
                break;
            case "DOUBLE_ARROW":
                return new Plots.DoubleArrow(points, params);
                break;
            case "FINE_ARROW":
                return new Plots.FineArrow(points, params);
                break;
            case "SQUAD_COMBAT":
                return new Plots.SquadCombat(points, params);
                break;
            case "ASSAULT_DIRECTION":
                return new Plots.AssaultDirection(points, params);
                break;
            case "STRAIGHT_ARROW":
                return new Plots.StraightArrow(points, params);
                break;
            case "TAILED_ATTACK_ARROW":
                return new Plots.TailedAttackArrow(points, params);
                break;
            case "TAILED_SQUAD_COMBAT":
                return new Plots.TailedSquadCombat(points, params);

        }
    }

    /**
     * 激活工具
     * @param type
     * @param params
     */
    active(type, params = {}) {
      
        let that = this;
        that.disActive();
        that.plotType = type;
        that.plotParams = params;
        if (!that.handler) {
            that.handler = new Cesium.ScreenSpaceEventHandler(
                that.viewer.scene.canvas
            );
        }
        // 左键单击事件：
        that.handler.setInputAction(function (e) {
          
            let cartesian1 = that.viewer.scene.pickPosition(e.position)
            let ray = that.viewer.scene.camera.getPickRay(e.position, new Cesium.Ray())
            let cartesian2 = that.viewer.scene.globe.pick(ray, that.viewer.scene, new Cesium.Cartesian3())
            let cartesian = cartesian1 || cartesian2;
            var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
            let lat = Cesium.Math.toDegrees(cartographic.latitude);
            let lng = Cesium.Math.toDegrees(cartographic.longitude);
            // first
            if (!that.plot) {
                that.plot = that.createPlot(type, that.points, params);
                let entities = that.plot.getEntities();
                if (entities instanceof Array) {
                    entities.forEach((entity) => {
                        that.plotDataSource.entities.add(entity);
                    });
                } else {
                    that.plotDataSource.entities.add(entities)
                }
                that.points.push([lng, lat]);
                that.plot.setPoints(that.points);
                if (that.plot.getPlotType() === "POINT") {
                    that.handler.destroy();
                    that.handler = null
                }
            } else {
                if (!that.plot.freehand) {
                    if (PlotUtils.distance([lng, lat], that.points[that.points.length - 1]) < 0.0001) {
                        return false;
                    }
                }
                that.points.push([lng, lat]);
                that.plot.setPoints(that.points);
                if (that.plot.fixPointCount === that.plot.getPointCount()) {
                    that.handler.destroy();
                    that.handler = null
                }
                if (that.plot.freehand) {
                    that.handler.destroy();
                    that.handler = null
                }
            }
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

        // 左键按下事件
        // eslint-disable-next-line no-unused-vars
        that.handler.setInputAction(function (e) { },
            Cesium.ScreenSpaceEventType.LEFT_DOWN);

        // 左键弹起事件
        // eslint-disable-next-line no-unused-vars
        that.handler.setInputAction(function (e) { },
            Cesium.ScreenSpaceEventType.LEFT_UP);

        // 左键双击事件：
        // eslint-disable-next-line no-unused-vars

        that.handler.setInputAction(function (e) {
            if (that.handler) {
                that.handler.destroy();
                that.handler = null
            }
        }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);

        // 右键单击事件
        // eslint-disable-next-line no-unused-vars
        that.handler.setInputAction(function (e) {
            if (that.handler) {
                that.handler.destroy();
                that.handler = null
            }
        }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);

        // 右键按下事件
        // eslint-disable-next-line no-unused-vars
        that.handler.setInputAction(function (e) { },
            Cesium.ScreenSpaceEventType.RIGHT_DOWN);

        // 右键弹起事件
        // eslint-disable-next-line no-unused-vars
        that.handler.setInputAction(function (e) { },
            Cesium.ScreenSpaceEventType.RIGHT_UP);

        // 移动事件：
        this.handler.setInputAction(function (e) {
            if (that.plot) {


                let cartesian1 = that.viewer.scene.pickPosition(e.endPosition);

                let ray = that.viewer.scene.camera.getPickRay(e.endPosition, new Cesium.Ray())
                let cartesian2 = that.viewer.scene.globe.pick(ray, that.viewer.scene, new Cesium.Cartesian3())
                let cartesian = cartesian1 || cartesian2;
                var cartographic = Cesium.Cartographic.fromCartesian(cartesian);


                let lat = Cesium.Math.toDegrees(cartographic.latitude);
                let lng = Cesium.Math.toDegrees(cartographic.longitude);


                if (PlotUtils.distance([lng, lat], that.points[that.points.length - 1]) < 0.0001) {
                    return false;
                }
                if (!that.plot.freehand) {
                    let pnts = that.points.concat([[lng, lat]]);
                    that.plot.setPoints(pnts);
                } else {
                    that.points.push([lng, lat]);
                    that.plot.setPoints(that.points);
                }
            }
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    }

    /**
     * 取消激活状态
     */
    disActive() {
        if (this.handler) {
            this.handler.destroy();
            this.handler = null
        }

        this.points = [];
        this.plot = null;
        this.feature = null;
        this.plotType = null;
        this.plotParams = null;
        this.handler = null;

    }

    /**
     * 激活交互工具
     */
    activeInteraction() {
        let that = this
        if (!this.handler) {
            this.handler = new Cesium.ScreenSpaceEventHandler(
                this.viewer.scene.canvas
            )
        }
        // 左键单击事件：
        this.handler.setInputAction(function (e) {
            that.plot.setClickHandler(that, e)
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK)

        // 左键双击事件：
        this.handler.setInputAction(function (e) {
            that.plot.setDoubleClickHandler(that, e)
        }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)

        // 移动事件：
        this.handler.setInputAction(function (e) {
            that.plot.setMouseMoveHandler(that, e)
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
    }



}


export default PlotDraw;